package com.apes.auto.autoparts.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.auto.autoparts.model.ZptLabelWhitelist;
import com.apes.auto.autoparts.repository.ZptLabelWhiteListRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.framework.util.EventUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.rbac.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lishikun on 2021/4/2 10:49
 */
@Service("autopartsService")
public class AutopartsService extends DomainService {

    @Autowired
    private PostRepository postRepository;
    @Autowired
    private ZptLabelWhiteListRepository zptLabelWhiteListRepository;


    public Object fuzzyOfParts(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        requestJO.put("stockValid", true);
        request.setData(requestJO);
        return fuzzyOfPartsReal(request);
    }

    /**
     * 查询真实的配件
     * @param request
     * @return
     */
    private Object fuzzyOfPartsReal(SimpleRequest request) {
        JSONObject jo = request.getJO();
        List<Map> filters = request.get("condition");
        JSONObject response = new JSONObject();
        String categoryId = "";
        Boolean isQuery = null;
        Boolean queryStock = jo.getBoolean("queryStock");
        Boolean display = true;
        JSONObject category = null;
        int matchingProductConfig = 0;
        for (Map filter : filters) {
            if ("searchContent".equals(filter.get("field"))) {
                category = ((JSONObject) filter.get("value")).getJSONObject("partsTag");
                categoryId = category.getString("id");
                if (category.getJSONObject("filterCondition") != null) {
                    matchingProductConfig = category.getJSONObject("filterCondition").getJSONObject("smartAccessoriesConfig").getIntValue("matchingProductConfig");
                    if (matchingProductConfig == 2) {
                        response.put("queryStock", queryStock);
                        response.put("display", false);
                        response.put("apes", MapUtil.mapper("elements", 0, "page", 0, "pages", 0, "size", 0, "total", 0, "content", new ArrayList<>()));
                        return response;
                    } else response.put("display", true);
                }
            }
            // 有无装配图  true 有装配图 控制查有库存；false 无装配图 控制查无库存
            if ("isQuery".equals(filter.get("field"))) isQuery = Boolean.valueOf(filter.get("value").toString());
            // 不匹配模式不控制库存


        }
        for (Map filter : filters) {
            // 前端传入的数据为空字符串情况
            if ("categoryTemp".equals(filter.get("field")) ) {
                if("".equals(categoryId)) {
                    response.put("queryStock", queryStock);
                    response.put("display", true);
                    return response;
                }
                filter.put("value", categoryId);
            }
        }
        // 获取 服务器模糊查询的结果
        jo.put("condition", filters);
        // 有无装配图  true 有装配图 控制查有库存；false 无装配图 控制查全部商品
        request.setData(jo);
        Object products = invoke("cm.product.findProductByServicePlan", request.getJO());
        response.put("apes", products);
        response.put("display", display);
        response.put("queryStock", queryStock);
        return response;
    }

    /**
     * 查询演示配件
     * @param request
     * @return
     */
    public Object fuzzyOfPartsDemo (SimpleRequest request) {
        List<Map> filters = request.get("condition");
        String vin = "";
        String keywords = "";
        for (Map filter : filters) {
            if ("dataCenterCondition".equals(filter.get("field"))) {
                vin = ((JSONObject) filter.get("value")).getString("vin");
                keywords = ((JSONObject) filter.get("value")).getString("keywords");
            }
        }
        return makePartsDemoData(vin, keywords);
    }

    /**
     * 构造演示配件数据
     * @param vin
     * @param keywords
     * @return
     */
    private JSONObject makePartsDemoData(String vin, String keywords) {
        String volkswagen = "{\"apes\":{\"total\":4,\"pages\":1,\"size\":20,\"elements\":4,\"page\":0,\"content\":[{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2020-05-29 21:56:48\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"description\":\"铱铂金\",\"stackLevel\":0,\"type\":\"product\",\"valid\":true,\"stockAmount\":5,\"uom\":{\"name\":\"支\",\"id\":1046404},\"subStock\":16,\"generalStock\":20,\"speAndModel\":\"/TORCH1\",\"usableQTY\":5,\"oemId\":\"TORCH1\",\"model\":\"TORCH1\",\"id\":\"0001133269\",\"state\":\"0\",\"orginID\":\"0001133269\",\"brand\":{\"name\":\"湘火炬\",\"id\":\"002611\"},\"createDate\":\"2020-05-26 21:42:26\",\"lastUpdatedStamp\":\"2020-05-29 21:56:48.467\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":2,\"innerBusinessKey\":\"0001133269\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"湘火炬铱铂金火花塞TORCH1\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 11:00:52\",\"originPlace\":\"美国\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"stackLevel\":0,\"type\":\"product\",\"specifications\":\"4支装\",\"valid\":true,\"stockAmount\":10,\"uom\":{\"name\":\"支\",\"id\":1362518},\"subStock\":12,\"generalStock\":312,\"speAndModel\":\"4支装/PFR7S8EG\",\"usableQTY\":9,\"oemId\":\"PFR7S8EG\",\"model\":\"PFR7S8EG\",\"id\":\"0000022071\",\"state\":\"0\",\"orginID\":\"0000022071\",\"brand\":{\"name\":\"NGK\",\"id\":\"000108\"},\"createDate\":\"2020-05-26 22:34:47\",\"lastUpdatedStamp\":\"2021-03-16 11:00:52.486\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":4,\"innerBusinessKey\":\"0000022071\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"NGK双铂金火花塞PFR7S8EG(1675)\",\"frozenQTY\":1,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 11:00:47\",\"originPlace\":\"山东\",\"transferReceiveQty\":5,\"requireBillingQty\":0,\"description\":\"火花塞铱金系列\",\"stackLevel\":5,\"type\":\"product\",\"specifications\":\"4支装\",\"valid\":false,\"stockAmount\":20,\"uom\":{\"name\":\"支\",\"id\":1069777},\"subStock\":36,\"generalStock\":16,\"speAndModel\":\"4支装/XH30006\",\"usableQTY\":2,\"oemId\":\"XH30006\",\"model\":\"XH30006\",\"id\":\"0000021955\",\"state\":\"0\",\"orginID\":\"0000021955\",\"brand\":{\"name\":\"德尔福\",\"id\":\"000680\"},\"createDate\":\"2020-05-26 21:46:14\",\"lastUpdatedStamp\":\"2021-03-16 11:00:47.152\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":4,\"innerBusinessKey\":\"0000021955\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"德尔福铱金火花塞XH30006\",\"frozenQTY\":13,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 14:04:18\",\"originPlace\":\"日本\",\"transferReceiveQty\":2,\"requireBillingQty\":2,\"description\":\"铂金\",\"stackLevel\":5,\"type\":\"product\",\"specifications\":\"4支装\",\"valid\":true,\"stockAmount\":4,\"uom\":{\"name\":\"支\",\"id\":1258621},\"subStock\":14,\"generalStock\":4,\"speAndModel\":\"4支装/PK20TT\",\"usableQTY\":0,\"oemId\":\"PK20TT\",\"model\":\"PK20TT\",\"id\":\"0001028439\",\"state\":\"0\",\"orginID\":\"0001028439\",\"brand\":{\"name\":\"电装\",\"id\":\"000719\"},\"createDate\":\"2020-05-26 22:17:27\",\"lastUpdatedStamp\":\"2021-03-16 14:04:18.551\",\"qualityGuaranteePeriod\":365,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":3,\"innerBusinessKey\":\"0001028439\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"电装铂金火花塞PK20TT(4504)\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}}]},\"display\":true,\"queryStock\":true}";
        String toyota = "{\"apes\":{\"total\":3,\"pages\":1,\"size\":20,\"elements\":3,\"page\":0,\"content\":[{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 14:55:43\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"description\":\"0\",\"stackLevel\":0,\"type\":\"product\",\"valid\":true,\"stockAmount\":8,\"uom\":{\"name\":\"支\",\"id\":1046410},\"subStock\":16,\"generalStock\":32,\"speAndModel\":\"/K6RTC\",\"usableQTY\":8,\"oemId\":\"K6RTC\",\"model\":\"K6RTC\",\"id\":\"0001133230\",\"state\":\"0\",\"orginID\":\"0001133230\",\"brand\":{\"name\":\"湘火炬\",\"id\":\"002611\"},\"createDate\":\"2020-05-26 21:42:26\",\"lastUpdatedStamp\":\"2021-03-16 14:55:43.250\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":3,\"innerBusinessKey\":\"0001133230\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"湘火炬火花塞K6RTC\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 11:13:09\",\"originPlace\":\"山东\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"description\":\"火花塞铱金系列\",\"stackLevel\":5,\"type\":\"product\",\"specifications\":\"4支装\",\"valid\":false,\"stockAmount\":6,\"uom\":{\"name\":\"支\",\"id\":1069196},\"subStock\":0,\"generalStock\":174,\"speAndModel\":\"4支装/XH30013\",\"usableQTY\":6,\"oemId\":\"XH30013\",\"model\":\"XH30013\",\"id\":\"0000021949\",\"state\":\"0\",\"orginID\":\"0000021949\",\"barcode\":\"6954497132819\",\"brand\":{\"name\":\"德尔福\",\"id\":\"000680\"},\"createDate\":\"2020-05-26 21:46:09\",\"lastUpdatedStamp\":\"2021-03-16 11:13:09.504\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":4,\"innerBusinessKey\":\"0000021949\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"德尔福铱金火花塞XH30013\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 11:13:01\",\"originPlace\":\"美国\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"stackLevel\":0,\"type\":\"product\",\"specifications\":\"4支装\",\"valid\":true,\"stockAmount\":0,\"uom\":{\"name\":\"支\",\"id\":1524253},\"subStock\":24,\"generalStock\":32,\"speAndModel\":\"4支装/IFR5T11\",\"usableQTY\":0,\"oemId\":\"IFR5T11\",\"model\":\"IFR5T11\",\"id\":\"0000021788\",\"state\":\"0\",\"orginID\":\"0000021788\",\"brand\":{\"name\":\"NGK\",\"id\":\"000108\"},\"createDate\":\"2020-05-26 23:01:45\",\"lastUpdatedStamp\":\"2021-03-16 11:13:01.897\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":5,\"innerBusinessKey\":\"0000021788\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"NGK铱铂金火花塞IFR5T11(4996)\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}}]},\"display\":true,\"queryStock\":true}";
        String honda = "{\"apes\":{\"total\":1,\"pages\":1,\"size\":20,\"elements\":1,\"page\":0,\"content\":[{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2020-10-23 22:15:21\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"stackLevel\":0,\"type\":\"product\",\"valid\":true,\"stockAmount\":0,\"uom\":{\"name\":\"支\",\"id\":1457819},\"subStock\":16,\"generalStock\":16,\"speAndModel\":\"/ILZKAR8H8S\",\"usableQTY\":0,\"oemId\":\"ILZKAR8H8S\",\"model\":\"ILZKAR8H8S\",\"id\":\"0001895231\",\"state\":\"0\",\"orginID\":\"0001895231\",\"brand\":{\"name\":\"NGK\",\"id\":\"000108\"},\"createDate\":\"2020-05-26 22:50:42\",\"lastUpdatedStamp\":\"2020-10-23 22:15:21.013\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":3,\"innerBusinessKey\":\"0001895231\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"NGK铱铂金火花塞ILZKAR8H8S(95112)\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}}]},\"display\":true,\"queryStock\":true}";
        String nissan ="{\"apes\":{\"total\":2,\"pages\":1,\"size\":20,\"elements\":2,\"page\":0,\"content\":[{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-05-28 20:59:40\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"stackLevel\":0,\"type\":\"product\",\"valid\":true,\"stockAmount\":8,\"uom\":{\"name\":\"支\",\"id\":99081},\"subStock\":12,\"generalStock\":554,\"speAndModel\":\"/VR7SPP33\",\"usableQTY\":8,\"oemId\":\"VR7SPP33;0242135524\",\"model\":\"VR7SPP33\",\"id\":\"0000021652\",\"state\":\"0\",\"orginID\":\"0000021652\",\"barcode\":\"4047024384354;4047024384361\",\"brand\":{\"name\":\"博世\",\"id\":\"000497\"},\"createDate\":\"2020-05-26 19:04:33\",\"lastUpdatedStamp\":\"2021-05-28 20:59:40.799\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":7,\"innerBusinessKey\":\"0000021652\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"博世双铂金火花塞VR7SPP33(0242135524)\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-10-11 10:43:40\",\"originPlace\":\"株洲\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"description\":\"铱金、侧电极带U型槽\",\"stackLevel\":10,\"type\":\"product\",\"specifications\":\"1支装\",\"valid\":true,\"stockAmount\":0,\"uom\":{\"name\":\"支\",\"id\":1046448},\"subStock\":16,\"generalStock\":217,\"speAndModel\":\"1支装/LD6RAIU\",\"usableQTY\":0,\"oemId\":\"LD6RAIU\",\"model\":\"LD6RAIU\",\"id\":\"0001508307\",\"state\":\"0\",\"orginID\":\"0001508307\",\"brand\":{\"name\":\"湘火炬\",\"id\":\"002611\"},\"createDate\":\"2020-05-26 21:42:26\",\"lastUpdatedStamp\":\"2021-10-11 10:43:40.498\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":5,\"innerBusinessKey\":\"0001508307\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"湘火炬铱金火花塞LD6RAIU\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}}]},\"display\":true,\"queryStock\":true}";
        String bmw = "{\"apes\":{\"total\":3,\"pages\":1,\"size\":20,\"elements\":3,\"page\":0,\"content\":[{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-05-28 20:59:40\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"stackLevel\":0,\"type\":\"product\",\"valid\":true,\"stockAmount\":8,\"uom\":{\"name\":\"支\",\"id\":98575},\"subStock\":43,\"generalStock\":409,\"speAndModel\":\"/ZR5TPP330\",\"usableQTY\":8,\"oemId\":\"ZR5TPP330;0242145541\",\"model\":\"ZR5TPP330\",\"id\":\"0000021726\",\"state\":\"0\",\"orginID\":\"0000021726\",\"barcode\":\"4047025301640;4047025301657\",\"brand\":{\"name\":\"博世\",\"id\":\"000497\"},\"createDate\":\"2020-05-26 19:04:31\",\"lastUpdatedStamp\":\"2021-05-28 20:59:40.823\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":7,\"innerBusinessKey\":\"0000021726\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"博世双铂金火花塞ZR5TPP330(0242145541)\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2020-05-29 22:02:05\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"description\":\"铱铂金\",\"stackLevel\":0,\"type\":\"product\",\"valid\":true,\"stockAmount\":7,\"uom\":{\"name\":\"支\",\"id\":1046852},\"subStock\":2,\"generalStock\":22,\"speAndModel\":\"/B1\",\"usableQTY\":7,\"oemId\":\"B1\",\"model\":\"B1\",\"id\":\"0001133292\",\"state\":\"0\",\"orginID\":\"0001133292\",\"brand\":{\"name\":\"湘火炬\",\"id\":\"002611\"},\"createDate\":\"2020-05-26 21:42:28\",\"lastUpdatedStamp\":\"2020-05-29 22:02:05.759\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":2,\"innerBusinessKey\":\"0001133292\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"湘火炬铱铂金火花塞B1\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}},{\"classify\":{\"name\":\"火花塞\",\"id\":\"M01010104003\"},\"updateDate\":\"2021-03-16 11:00:51\",\"originPlace\":\"美国\",\"transferReceiveQty\":0,\"requireBillingQty\":0,\"stackLevel\":0,\"type\":\"product\",\"specifications\":\"4支装\",\"valid\":true,\"stockAmount\":6,\"uom\":{\"name\":\"支\",\"id\":1362515},\"subStock\":10,\"generalStock\":16,\"speAndModel\":\"4支装/SILZKBR8D8S\",\"usableQTY\":6,\"oemId\":\"SILZKBR8D8S\",\"model\":\"SILZKBR8D8S\",\"id\":\"0000022150\",\"state\":\"0\",\"orginID\":\"0000022150\",\"brand\":{\"name\":\"NGK\",\"id\":\"000108\"},\"createDate\":\"2020-05-26 22:34:47\",\"lastUpdatedStamp\":\"2021-03-16 11:00:51.860\",\"qualityGuaranteePeriod\":0,\"freeSuppliesValid\":false,\"tax\":{\"name\":\"13%增值税\",\"amt\":13,\"id\":6},\"freeServiceValid\":false,\"inquiryValid\":false,\"version\":4,\"innerBusinessKey\":\"0000022150\",\"productGroup\":{\"name\":\"一般商品\",\"id\":\"10\"},\"instruction\":\"4支/盒\",\"transferInBillingQty\":0,\"purchasingWayQty\":0,\"name\":\"NGK铱铂金火花塞SILZKBR8D8S(97506)\",\"frozenQTY\":0,\"category\":{\"name\":\"火花塞\",\"id\":\"000615\"},\"innerBusinessType\":\"Product\",\"fitting\":{\"name\":\"火花塞\",\"id\":\"000615\"}}]},\"display\":true,\"queryStock\":true}";
        String noStock = "{\"apes\":{\"total\":0,\"pages\":0,\"size\":20,\"elements\":0,\"page\":0,\"content\":[]},\"display\":true,\"queryStock\":true}";
        if ("火花塞".equals(keywords)) {
          if ("LSVCZ6A41FN194814".equals(vin)) // 大众
              return JSONObject.parseObject(volkswagen);
          else if ("LFMKV36F5C0131871".equals(vin)) //丰田
              return JSONObject.parseObject(toyota);
          else if ("LHGTG3881H8018823".equals(vin)) // 本田
              return JSONObject.parseObject(honda);
          else if ("LJNMDV1L3GN602877".equals(vin)) // 日产
              return JSONObject.parseObject(nissan);
          else if("LBVUG710XFMD28709".equals(vin)) //宝马
              return JSONObject.parseObject(bmw);
          else return JSONObject.parseObject(noStock);
        }
        return JSONObject.parseObject(noStock);
    }

    @Listener(topic = "event:autoparts.saveLog")
    private void saveLog(SimpleRequest request) {
        JSONObject jo = request.getJO();
        JSONObject param = new JSONObject();
        Dept dept = postRepository.findOne(jo.getString("postId")).getDept();
        Boolean inventoryCommodity = null;
        Boolean ZPCommodity = null;
        Boolean universalCommodity = null;
        if( jo.getJSONObject("request").getBooleanValue("queryStock") ) {
            if (jo.getJSONObject("response").getJSONObject("apes").getJSONArray("content").size() > 0)
                inventoryCommodity = true;
            else inventoryCommodity = false;
        } else {
            if (jo.getJSONObject("response").getJSONObject("apes").getJSONArray("content").size() > 0)
                universalCommodity = true;
            else universalCommodity = false;
        }

        param.put("userId", jo.getJSONObject("user").getString("id"));
        param.put("userName", jo.getJSONObject("user").getString("name"));
        param.put("deptId", dept.getId());
        param.put("deptNmae", dept.getName());
        param.put("license", jo.getJSONObject("request").getJSONObject("conditionModel").getJSONObject("customerCar").getString("carLicense"));
        param.put("vin", jo.getJSONObject("request").getJSONObject("conditionModel").getJSONObject("customerCar").getString("vin"));
        param.put("inputText", jo.getString("keywords"));
        param.put("inventoryCommodity", inventoryCommodity);
        param.put("ZPCommodity", ZPCommodity);
        param.put("universalCommodity", universalCommodity);
        invoke("apes.behaviorRecord.save",param);
    }

    public void searchZPT_PBFWTask(SimpleRequest request) {
        Object invoke = invoke("dataCenter.auto.parts.searchZPT_PBFW", new JSONObject());
        JSONArray data = JSONObject.parseObject(invoke.toString()).getJSONObject("body").getJSONArray("data");
        for (Object o : data) {
            JSONObject json = (JSONObject)o;
            if (zptLabelWhiteListRepository.findOne(json.getString("QCCPID")) == null) {
                ZptLabelWhitelist zptLabelWhitelist = new ZptLabelWhitelist();
                zptLabelWhitelist.setId(json.getString("QCCPID"));
                zptLabelWhitelist.setQccpmc(json.getString("QCCPMC"));
                zptLabelWhiteListRepository.saveAndFlush(zptLabelWhitelist);
            }
        }
    }



    private Boolean isConditionModel(JSONObject jsonObject) {
        return jsonObject.getJSONObject("conditionModel") == null;
    }



}
